package com.app.utils.util;

import android.app.ActivityManager;
import android.content.Context;
import android.text.ClipboardManager;
import android.text.TextUtils;

import java.io.File;
import java.util.List;

/**
 * 一些操作的辅助类
 * Created by xuxinghai on 2016/7/12.
 */
public class HelpUtils {

    /**
     * 将文字复制到剪切板
     *
     * @param content
     * @param context
     */
    public static void copy(String content, Context context) {
        if (TextUtils.isEmpty(content) || context == null) {
            return;
        }
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());

    }

    /**
     * 判断是否安装目标应用
     *
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     * @author xuxinghai
     */
    public static boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * 判断当前应用程序是否在前台运行
     *
     * @param context
     * @return
     */
    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    // 后台
                    return true;
                } else {
                    // 前台
                    return false;
                }
            }
        }
        return true;
    }

}
